package com.cn.xiaonuo.slbfinances.slcoupon.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.common.utils.DateTimeUtils;
import com.cn.xiaonuo.dbs.core.annotation.DataSource;
import com.cn.xiaonuo.slbfinances.slcoupon.mapper.CmExReleaseMapper;
import com.cn.xiaonuo.slbfinances.slcoupon.service.IHpcCouponService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HpcCouponServiceImpl extends ServiceImpl<CmExReleaseMapper,String> implements IHpcCouponService {

    @Resource
    private CmExReleaseMapper cmExReleaseMapper;

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage selectGwjInfo(String userId, Long beginTime, Long endTime, Integer pageNum, Integer pageSize) {
        BusinessMessage message = new BusinessMessage();
        try {
            if (null == pageNum || pageNum <= 1) {
                pageNum = 1;
            }
            if (null == pageSize || pageSize <= 1) {
                pageSize = 10;
            }
            String beginTimes = null;
            String endTimes = null;
            if (null != beginTime && null != endTime) {
                Map<String, String> map = DateTimeUtils.conversionTime(beginTime, endTime);
                beginTimes = map.get("begin");
                endTimes = map.get("end");
            }
            List<Map<String, Object>> resunum = cmExReleaseMapper.selectGwjInfo(userId, beginTimes, endTimes);
            PageHelper.startPage(pageNum, pageSize);  // 设置分页参数
            List<Map<String, Object>> resu = cmExReleaseMapper.selectGwjInfo(userId, beginTimes, endTimes);
            PageInfo<Map<String, Object>> sult = new PageInfo<>(resu);
            Map<String, Object> map = new HashMap<>(16);
            map.put("lastResult", sult);
            map.put("nums", null==resunum?0:resunum.size());
            message.setData(map);
            message.setCode("200");
            message.setSuccess(true);
            message.setMessage("操作成功");
        } catch (Exception e) {
            message.setSuccess(false);
            message.setMessage("操作失败");
            e.printStackTrace();
        }
        return message;
    }
}
